
Want your Arduino to react instantly to a button press? Then you need Arduino interrupts. In fact, this button-LED project shows how to use external interrupts to toggle an LED the moment you press a button—no delays, no polling, no wasted code. Moreover, it’s perfect for beginners who want to build fast, efficient, and event-driven projects.
This guide walks you through every step: parts list, wiring, clean code, and testing. Therefore, you’ll learn how Arduino interrupts work, why they beat traditional button-checking loops, and how to write a proper Interrupt Service Routine (ISR). So, let’s unlock real-time responsiveness with Arduino interrupts!
Why Use Arduino Interrupts?
Arduino interrupts let your code respond to events immediately. For example, when you press a button, the Arduino stops everything and runs a special function—called an ISR—right away. As a result, your project feels snappy and reliable.
Without interrupts, you’d use polling: constantly checking the button state inside loop(). But this wastes processor time and can miss fast button presses. By contrast, interrupts save power, simplify code, and guarantee instant response. Consequently, they’re essential for time-critical tasks like emergency stops, sensor triggers, or user inputs.
In this button-LED project, the main loop stays empty. All the action happens in the ISR. This proves how efficient Arduino interrupts can be—even for simple tasks.
Essential Parts for Arduino Interrupts Button LED Project
You only need a few basic components. First, grab an Arduino Uno or any compatible board. Second, get one LED—any color works. Third, prepare a 220Ω resistor to protect the LED from too much current.
Next, add a momentary push button and a 10kΩ resistor for the pull-up circuit. Also, use a breadboard and jumper wires to connect everything cleanly. Finally, power your Arduino with a standard USB cable.
💡 Tip: The Arduino Uno only supports hardware interrupts on pins 2 and 3. So, you must use one of these for your button. In this project, we use pin 2.
How to Wire the Arduino Interrupts Button LED Circuit
Start with the LED. Connect its anode (long leg) to Arduino pin 13 through a 220Ω resistor. Then, link the cathode (short leg) directly to GND. This powers the built-in LED on most Uno boards—or your external one.
Now, wire the button. Attach one side of the push button to Arduino digital pin 2. Connect the other side to GND. After that, place a 10kΩ resistor between pin 2 and the 5V rail. This pull-up resistor keeps the pin HIGH when the button is not pressed, and pulls it LOW when pressed.
Finally, plug in the USB cable to power the Arduino. Double-check all connections before uploading code. A wrong wire can cause the interrupt to fail or the LED to stay on.
💡 Tip: If you forget the pull-up resistor, the pin may float—causing random toggles. So, always include it, or use the Arduino’s internal pull-up (but this project uses external for clarity).
Arduino Code for Interrupts Button LED Project
// Define pin connections
#define LED_PIN 13 // LED connected to pin 13
#define BUTTON_PIN 2 // Button connected to interrupt pin 2
// 'volatile' is essential: it tells the compiler that this variable
// can be changed unexpectedly by the Interrupt Service Routine (ISR).
volatile bool ledState = false;
void setup() {
pinMode(LED_PIN, OUTPUT);
// Set button pin as input with internal pull-up disabled (we use an external pull-up)
pinMode(BUTTON_PIN, INPUT);
// Attach the interrupt:
// 1. digitalPinToInterrupt(BUTTON_PIN): Converts pin number 2 to its interrupt number (0).
// 2. toggleLED: The function (ISR) to call when the interrupt is triggered.
// 3. FALLING: Trigger the interrupt when the pin goes from HIGH (pull-up state) to LOW (button pressed).
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), toggleLED, FALLING);
}
void loop() {
// The main loop is empty. The LED logic is handled entirely by the interrupt.
// This demonstrates the efficiency of interrupt-driven programming.
}
// Interrupt Service Routine (ISR)
void toggleLED() {
// Critical ISR Best Practices:
// 1. Keep it short.
// 2. Do not use 'delay()'.
// 3. Do not use 'Serial' communication.
ledState = !ledState; // Toggle the state flag
digitalWrite(LED_PIN, ledState); // Update the LED immediately
}
How the Arduino Interrupts Code Works
The code starts with clear pin definitions. LED_PIN is 13, and BUTTON_PIN is 2. This makes the code easy to read and modify. Next, the ledState variable is declared as volatile. Why? Because it changes inside the ISR, and the compiler must never optimize it away.
In setup(), pin 13 becomes an OUTPUT for the LED. Pin 2 is set as INPUT (not INPUT_PULLUP) because we use an external 10kΩ pull-up resistor. Then, attachInterrupt() links the button to the toggleLED function. The FALLING mode means the ISR runs only when the pin drops from HIGH to LOW—i.e., when the button is pressed.
The loop() function is completely empty. This shows the power of Arduino interrupts: the Arduino doesn’t waste time checking the button. Instead, it waits silently until an interrupt occurs. As a result, the processor is free for other tasks in more complex projects.
Finally, the toggleLED() function is the ISR. It flips the ledState value and updates the LED instantly. Note: ISRs must be fast. So, we avoid delay(), Serial.print(), or complex math. This keeps the system responsive and stable.
Testing Your Arduino Interrupts Button LED Project
After uploading the code, test the circuit. First, the LED should be off. Then, press the button once—the LED should turn on immediately. Press it again, and it should turn off. Each press toggles the state.
If the LED doesn’t toggle, check three things. First, confirm you’re using pin 2 or 3 for the button—only these support external interrupts on the Uno. Second, verify the 10kΩ resistor connects between pin 2 and 5V. Third, ensure the button connects pin 2 to GND when pressed.
Also, watch for double-toggles. If the LED flips twice on one press, your button is bouncing. To fix this, add a small capacitor (0.1µF) across the button, or use software debouncing—but that’s advanced. For now, press the button firmly and quickly.
💡 Tip: Try removing the resistor. Without it, the pin floats, and the LED may toggle randomly. This shows why pull-up resistors are essential in Arduino interrupts projects.
Common Mistakes in Arduino Interrupts Projects
One big error is using non-interrupt pins. Remember: on the Uno, only pins 2 and 3 work for external interrupts. So, never connect your button to pin 4 or 5 and expect interrupts to fire.
Another mistake is forgetting the volatile keyword. If you skip it, the compiler might assume ledState never changes in loop()—so the LED won’t update correctly. Always declare ISR-modified variables as volatile.
Also, avoid using delay() or Serial in the ISR. These functions can hang the processor or cause crashes. Keep ISRs short, simple, and fast. Set a flag in the ISR, and handle complex logic in loop() if needed—but this project doesn’t require it.
Finally, don’t use INPUT_PULLUP if you already have an external pull-up. It can create a voltage divider and weaken the signal. Choose one method—external resistor or internal pull-up—but not both.
How to Expand Your Arduino Interrupts Skills
Once this project works, try new ideas. First, add a second button on pin 3 to control another LED. Use a second ISR to toggle it independently. This teaches multi-interrupt handling.
Second, use interrupts with sensors. For example, connect a tilt switch or PIR motion sensor to pin 2. When motion is detected, the ISR can trigger an alarm or log data. This turns your project into a security system.
Third, combine interrupts with timers. Use a timer interrupt to blink an LED every second, while an external interrupt toggles a mode. This shows how different interrupt types work together.
Moreover, explore pin change interrupts (available on all pins) for more flexibility. But start with external interrupts—they’re the easiest and most reliable for beginners.
Why This Arduino Interrupts Project Is Perfect for Beginners
This project uses minimal parts and simple code. Yet, it teaches a powerful concept: event-driven programming. You see instant results, which builds confidence fast. Also, the empty loop() surprises many learners—it shows how efficient interrupts can be.
Teachers use this in classrooms because it’s safe, cheap, and visual. Students press a button and see immediate feedback. Therefore, it bridges theory and practice in a fun, memorable way.
Furthermore, it lays the foundation for robotics, home automation, and IoT. Once you master Arduino interrupts, you can build systems that react to doors opening, lights changing, or buttons pressing—without missing a beat.
Final Thoughts: Master Arduino Interrupts Today
This Arduino interrupts button LED project proves that real-time control doesn’t require complex code. With just two pins, a button, and an LED, you’ve learned how to make your Arduino respond instantly to the real world.
So, build it, test it, and then imagine bigger projects. Add more buttons, sensors, or outputs. Before long, you’ll design systems that feel alive—because they react the moment something happens.
Remember: the key to great embedded systems is responsiveness. And Arduino interrupts are your first step toward that goal.







Leave a Reply